﻿using System;
using System.Data.Common;
using Ext.Net;
using Ext.Net.Utilities;
using KamLib;

namespace KamLib
{
    public class DbUpdater
    {
        public string StatusMessage;
        public int VerNo;
        public DbConnection cn;
        public DbTransaction tran;

        public void Exec(DbConnection connection, Action[] updates, int startVerNo)
        {
            int lastVerNo = 0;
            cn = connection;
            try
            {
                VerNo = cn.ExecInt("select DBVer from tVer");
                var priorVerNo = VerNo;
                var len = updates.Length;
                if (VerNo - startVerNo >= len)
                {
                    //"The database is last version.";
                    return;
                }
                if (VerNo < startVerNo) VerNo = startVerNo;



                try
                {
                    for (var i = VerNo - startVerNo; i < len; ++i)
                    {
                        lastVerNo = VerNo + 1;
                        try
                        {
                            tran = cn.BeginTransaction();
                            updates[i]();
                            tran.Commit();
                            VerNo++;
                        }
                        catch (Exception)
                        {
                            tran.Rollback();
                            throw;
                        }
                    }
                }
                finally
                {
                    if (VerNo > priorVerNo)
                        cn.Exec("update tVer set DBVer = " + VerNo);
                }

                StatusMessage = @"<div class=""dbupdate-ok"">Database successfull updated from version " + priorVerNo + " to " + VerNo + ".</div>";
            }
            catch (Exception ex)
            {
                StatusMessage = @"<div class=""dbupdate-error""><h1>Update Database #" + lastVerNo + "</h1>" + ex.FullMessage().Replace("\r\n", "<br/>") + "</div>";
            }
        }


        public void Exec(string cmd)
        {
            cn.Exec(tran, cmd);
        }

        public void Exec(string cmd, params object[] @params)
        {
            cn.Exec(tran, cmd, @params);
        }


        public static void ShowNotification(string msg)
        {
            if (msg.IsEmpty()) return;

            var isError = msg.Contains("error");

            if (isError)
            {
                Notification.Show(new NotificationConfig
                {
                    Title = "DbUpdate",
                    Icon = Icon.Error,
                    Html = msg,
                    AutoScroll = true,
                    Pinned = true,
                    ShowPin = true,
                    Width = 320,
                    Height = 200,
                });
            }
            else
            {
                Notification.Show(new NotificationConfig
                {
                    Title = "DbUpdate",
                    Icon = Icon.Information,
                    Html = msg,
                });
            }

        }
    }
}